
import '../../../../core/app_export.dart';
import '../../../../widgets/app_bar/appbar_subtitle.dart';
import '../../../../widgets/app_bar/custom_app_bar.dart';
import '../../../../widgets/app_bar/outline_icon_button.dart';
import '../../../../widgets/custom_search_view.dart';
import '../../../../widgets/screen_state_widget.dart';
import '../../add_player_page/bloc/add_player_page_screen.dart';
import '../widgets/player_item_view.dart';
import 'all_players_cubit.dart';
import 'package:flutter/material.dart';

class AllPlayersScreen extends StatelessWidget {
  const AllPlayersScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    double height = MediaQuery.of(context).size.height;
    double width = MediaQuery.of(context).size.width;

    return BlocProvider<PlayersTabCubit>(
      create: (context) => PlayersTabCubit()..getAllUsers(),
      child: BlocConsumer<PlayersTabCubit, PlayersTabState>(
        listener: (context, state) {
          // TODO: implement listener
        },
        builder: (context, state) {
          var cubit = PlayersTabCubit.get(context);

          return Scaffold(
            appBar: _buildAppBar(context: context,cubit: cubit),
            body: Container(
              height: height,
              width: width,
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage(ImageConstant.imgBackground),
                  fit: BoxFit.cover,
                ),
              ),
              child: Column(
                children: [
                  if(cubit.searchFlag)
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: CustomSearchView(
                        controller: cubit.searchBox,
                        hintText: 'ابحث عن اسم او رقم اللاعب',
                        //textDirection:
                        onChanged: (value){
                          cubit.onSearch(value);
                        },
                        onClear: (){
                          cubit.onSearch('');
                        },
                      ),
                    ),

                  Expanded(
                    child: ScreenStateWidget(
                      isDone: state is PlayersTabDone,
                      isError: state is PlayersTabError,
                      isLoading: state is PlayersTabLoading,
                      onRefresh: cubit.getAllUsers,
                      onDone: ListView.builder(
                          itemCount: cubit.users.length,
                          itemBuilder: (context,index){
                            return PlayerItemView(user: cubit.users[index],index: index,);
                          }),
                    ),
                  ),
                ],
              ),
            ),
          );
        },
      ),
    );
  }

  PreferredSizeWidget _buildAppBar({required BuildContext context , required PlayersTabCubit cubit}) {
    return CustomAppBar(
      actions: [
        if(PrefUtils.isAdmin())
          OutlineIconButton(
          icon: Icons.add,
          margin:const EdgeInsets.all(8),
          padding:const EdgeInsets.all(2),
          onTapped: ()async{
            await Navigator.push(context, MaterialPageRoute(builder: (context) => const AddPlayerPageScreen())).then((value) {
              //cubit.getAllUsers();
            });
          },
        )
      ],
      leadingWidth: 120,
      leading: Row(
        children: [
          OutlineIconButton(
            imagePath: ImageConstant.imgRightIcon,
            onTapped: (){
              Navigator.pop(context);
            },
          ),
          Stack(
            children: [
              if(cubit.searchBox.text.isNotEmpty)
              const Padding(
                padding: EdgeInsets.all(8.0),
                child: Align(
                  alignment: Alignment.bottomLeft,
                  child: CircleAvatar(
                    radius: 6,
                    backgroundColor: Colors.green,
                  ),
                ),
              ),
              Center(
                child: OutlineIconButton(
                  icon:cubit.searchFlag?Icons.search_off:Icons.search,
                  margin:const EdgeInsets.all(8),
                  padding:const EdgeInsets.all(2),
                  onTapped: ()async{
                    cubit.onSearchTap();
                  },
                ),
              ),
            ],
          ),
        ],
      ),
      title: AppbarSubtitle(text: 'اللاعبين'),
    );
  }
}
